/**
 * @ClassName KP38_QickSort
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/11/2 16:55
 */

import java.util.Arrays;
import java.util.LinkedList;

/**
 * 快速排序
 */
public class KP38_QuickSort {

    /**
     * 快速排序递归方式
     *
     * @param arr
     */
    public static void quickSortRecursive(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        quickSortRecursiveChild(arr, 0, arr.length - 1);
    }

    public static void quickSortRecursiveChild(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        //int div = partion1(arr, left, right);
        int div = partion2(arr, left, right);
        quickSortRecursiveChild(arr, left, div);
        quickSortRecursiveChild(arr, div + 1, right);
    }

    // Hoare法
    public static int partion1(int[] arr, int left, int right) {
        int i = left;
        int j = right;
        int key = arr[left];
        while (i < j) {
            while (i < j && arr[j] >= key) {
                j--;
            }
            while (i < j && arr[i] <= key) {
                i++;
            }
            swap(arr, i, j);
        }
        swap(arr, left, i);
        return i;
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    // 挖坑法
    public static int partion2(int[] arr, int left, int right) {
        int i = left;
        int j = right;
        int key = arr[left];
        while (i < j) {
            while (i < j && arr[j] >= key) {
                j--;
            }
            arr[i] = arr[j];
            while (i < j && arr[i] <= key) {
                i++;
            }
            arr[j] = arr[i];
        }
        arr[i] = key;
        return i;
    }


    /**
     * 快速排序非递归方式
     *
     * @param arr
     */
    public static void quickSortNonRecursive(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        quickSortNonRecursiveChild(arr, 0, arr.length - 1);
    }

    public static void quickSortNonRecursiveChild(int[] arr, int left, int right) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.addFirst(left);
        stack.addFirst(right);
        while (!stack.isEmpty()) {
            right = stack.pollFirst();
            left = stack.pollFirst();
            if (left >= right) {
                continue;
            }
            int div = partion1(arr, left, right);
            stack.addFirst(div + 1);
            stack.addFirst(right);
            stack.addFirst(left);
            stack.addFirst(div);
        }
    }

    /**
     * 判断两个数组是否相同
     *
     * @param arr1
     * @param arr2
     * @return
     */
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1 == null ^ arr2 == null || arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 生成随机函数
     *
     * @param maxSize
     * @param maxValue
     * @return
     */
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] array = new int[(int) (Math.random() * (maxSize + 1))];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) ((Math.random() * (maxValue + 1)) - (Math.random() * maxValue));
        }
        return array;
    }

    public static void test() {
        int testTimes = 1000000;
        int maxSize = 100;
        int maxValue = 100;
        System.out.println("测试开始");
        for (int i = 0; i < testTimes; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = Arrays.copyOf(arr1, arr1.length);
            // 递归算法
            //quickSortRecursive(arr1);
            // 非递归算法
            quickSortNonRecursive(arr1);
            Arrays.sort(arr2);
            if (!isEqual(arr1, arr2)) {
                System.out.println("结果出错了");
                System.out.println(Arrays.toString(arr1));
                System.out.println(Arrays.toString(arr2));
                break;
            }
        }
        System.out.println("测试结束");
    }

    public static void main(String[] args) {
        test();
    }
}
